﻿$(function () {

    var windowsManager = {
        ratio: 1.4,
        minHeight: 400,

        init: function () {
            this.discoverChildren();
            this.bindEvents();
            this._updatelayout();
            this._setCurrentLanguage();
            this._tryLockOrientation('portrait');
            this._warnIfStorageIsNotAvailable();
        },

        discoverChildren: function () {
            this.$innerbody = $("#innerbody");
        },

        bindEvents: function () {
            $(window).on('resize', this.onResize.bind(this));
            $(window).on('blur', this.onBlur.bind(this));
            $(window).on('focus', this.onFocus.bind(this));
            $(document.body).on('touchmove', this.onBodyScroll.bind(this));
        },

        onBlur: function (ev) {
            EVENTBROKER.matchTopic.trigger(TOPIC_EVENTS.matchRequestPause);
        },

        onFocus: function (ev) {
            EVENTBROKER.matchTopic.trigger(TOPIC_EVENTS.matchRequestResume);
        },

        onBodyScroll: function (ev) {
            ev.preventDefault();
        },

        onResize: function (ev) {
            this._updatelayout();
        },

        _updatelayout: function () {
            var dimensions = this._getAdaptedDimensions({
                originalHeight: $(window).height(),
                originalWidth: $(window).width()
            });

            this.$innerbody.css('height', dimensions.height);
            this.$innerbody.css('width', dimensions.width);

            $('body').css('font-size', dimensions.width / 15);
        },

        _getAdaptedDimensions: function (dims) {

            var perfectWidth = Math.max(dims.originalWidth, this.minHeight / this.ratio);
            var perfectHeight = perfectWidth * this.ratio;

            if (perfectHeight > dims.originalHeight) {
                perfectHeight = Math.max(dims.originalHeight, this.minHeight);
                perfectWidth = perfectHeight / this.ratio;
            }

            return {
                width: perfectWidth,
                height: perfectHeight
            }
        },

        _tryLockOrientation: function (orientation) {
            var screen = window.screen;
            var lockOrientation = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation;

            try {
                // Ie + ff
                if (lockOrientation) {
                    lockOrientation = lockOrientation.bind(screen);
                    lockOrientation(orientation);
                }

                //chrome
                if (screen.orientation) {
                    var promise = screen.orientation.lock(orientation);
                    promise['catch'](function (ex) { console.log(ex); })
                }
            } catch (e) {
                console.log(e)
            }
        },

        _setCurrentLanguage: function () {
            var lang = window.navigator.languages ? window.navigator.languages[0] : null;
            lang = lang || window.navigator.language || window.navigator.browserLanguage || window.navigator.userLanguage;
            if (lang.indexOf('-') !== -1)
                lang = lang.split('-')[0];

            if (lang.indexOf('_') !== -1)
                lang = lang.split('_')[0];

            Resx.setCurrentLang(lang);
            this._setUiLanguage();

        },
        _setUiLanguage: function () {
            $("[data-resx]").each(function (i, el) {
                var key = $(el).attr("data-resx");
                $(el).html(Resx.get(key));
            });
        },

        _warnIfStorageIsNotAvailable: function () {

            window.setTimeout(function () {
                try {
                    localStorage.setItem('test', 'test');
                    localStorage.removeItem('test');
                    return true;
                } catch (e) {
                    var errText = "Local storage is not available.<br/><br/> Please disable 'Private Mode' or use a modern Browser !";
                    $("#footer").html(errText);
                   
                }
            }, 100);
        }
    }

    windowsManager.init();

});
